package com.easemob.helpdesk.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.helpdesk.activity.MainActivity;
import com.easemob.helpdesk.bean.AdminVillage;
import com.easemob.helpdesk.bean.AdminVillageRes;
import com.easemob.helpdesk.bean.UserBuildRes;
import com.easemob.helpdesk.utils.CommonUtils;
import com.easemob.helpdesk.utils.DisplayUtil;
import com.easemob.helpdesk.widget.pickerview.SimplePickerView;
import com.housekeeperapp.BuildConfig;
import com.housekeeperapp.R;
import com.net.ApiService;
import com.net.BaseResponse;
import com.net.HKHttpUtils;
import com.net.HKResponseResultCallBack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by wsj on 2018/3/30.
 */

public class AddrSearchView implements SimplePickerView.SimplePickSelectItemListener {

    private Context mContext;
    private Object mParent;
    private boolean showStatus;

    private ViewGroup decorView;
    private ViewGroup rootView;

    private String curCity = "";
    private String curCityPinyin = "";
    private String curVillage = "";
    private String curBuilding = "请选择";
    private String curUnit = "请选择";
    private String curRoom = "";

    private List<AdminVillage> mList;
    private VillageAdapter mAdapter;

    private SimplePickerView simplePickerView;
    private ArrayList<String> building =new ArrayList<>();
    private ArrayList<String> unit = new ArrayList<>();
    private ArrayList<String> room = new ArrayList<>();

    private InputMethodManager inputMethodManager;
    private MaxHeightScrollView scrollView;

    public AddrSearchView(Context context, Object parent, boolean showStatus){
        this.mContext = context;
        this.mParent = parent;
        this.showStatus = showStatus;

        inputMethodManager = (InputMethodManager) ((Activity)mContext).getSystemService(Context.INPUT_METHOD_SERVICE);

        mList = new ArrayList<>();
//        building.add("请选择");
//        unit.add("请选择");

    }

    public void addLayoutListener(final View main, final View scroll) {
        main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int windowHeight = mContext.getResources().getDisplayMetrics().heightPixels;
                if ((scrollView.getHeight() < (windowHeight * 2 / 3))){
                    return;
                }
                Rect rect = new Rect();
                //1、获取main在窗体的可视区域
                main.getWindowVisibleDisplayFrame(rect);
                //2、获取main在窗体的不可视区域高度，在键盘没有弹起时，main.getRootView().getHeight()调节度应该和rect.bottom高度一样
                int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;
                int screenHeight = main.getRootView().getHeight();//屏幕高度
                //3、不可见区域大于屏幕本身高度的1/4：说明键盘弹起了
                if (mainInvisibleHeight > screenHeight / 4) {
                    int[] location = new int[2];
                    scroll.getLocationInWindow(location);
                    // 4､获取Scroll的窗体坐标，算出main需要滚动的高度
                    int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
                    //5､让界面整体上移键盘的高度
                    main.scrollTo(0, srollHeight);
                } else {
                    //3、不可见区域小于屏幕高度1/4时,说明键盘隐藏了，把界面下移，移回到原有高度
                    main.scrollTo(0, 0);
                }
            }
        });
    }

    public void showSearchView(){
        if (decorView != null && isShowing()) {
            hideSearchView();
            return;
        }

        if(decorView!=null && rootView!=null){
            rootView.setVisibility(View.VISIBLE);
            return;
        }

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        decorView = (ViewGroup) ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
        rootView = (ViewGroup) layoutInflater.inflate(R.layout.popup_village_filter, decorView, false);

        FrameLayout frameLayout = (FrameLayout) rootView.findViewById(R.id.popup_vill_bg);
        ListView villageList = (ListView) rootView.findViewById(R.id.village_list);
        mAdapter = new VillageAdapter();
        villageList.setAdapter(mAdapter);

        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSearchView();
            }
        });

        RelativeLayout rl_building = (RelativeLayout) rootView.findViewById(R.id.rl_building);
        RelativeLayout rl_unit = (RelativeLayout) rootView.findViewById(R.id.rl_unit);
        RelativeLayout rl_room = (RelativeLayout) rootView.findViewById(R.id.rl_room);
        scrollView = (MaxHeightScrollView) rootView.findViewById(R.id.scrollview);
        LinearLayout rl_keeper_status = (LinearLayout) rootView.findViewById(R.id.rl_keeper_status);

        final TextView rl_building_text = (TextView) rootView.findViewById(R.id.tv_building_text);
        final TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
        final EditText rl_room_text = (EditText) rootView.findViewById(R.id.tv_room_text);

        Button rl_clear_btn = (Button) rootView.findViewById(R.id.clear_data_btn);
        Button rl_ok_data_btn = (Button) rootView.findViewById(R.id.ok_data_btn);

        final RadioButton rl_radio0 = (RadioButton) rootView.findViewById(R.id.radio0);
        final RadioButton rl_radio1 = (RadioButton) rootView.findViewById(R.id.radio1);
        final RadioButton rl_radio2 = (RadioButton) rootView.findViewById(R.id.radio2);


//        int windowHeight = mContext.getResources().getDisplayMetrics().heightPixels;
//        int outerHeight = 0;
//        outerHeight = DisplayUtil.px2dip(mContext, windowHeight)-54;
//        ViewGroup.LayoutParams layoutlp = scrollView.getLayoutParams();
//        layoutlp.height = DisplayUtil.dip2px(mContext,outerHeight - 114);
//        scrollView.setLayoutParams(layoutlp);

        if(showStatus){
            rl_room.setVisibility(View.GONE);
            rl_keeper_status.setVisibility(View.VISIBLE);
        }else{
            rl_room.setVisibility(View.VISIBLE);
            rl_keeper_status.setVisibility(View.GONE);
        }

        rl_building_text.setTag("build");
        rl_unit_text.setTag("unit");
        rl_building.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(building.size() == 0){
                    return;
                }

                simplePickerView = new SimplePickerView(mContext, AddrSearchView.this, building, rl_building_text, "楼号");
                simplePickerView.setCancelable(true);
                simplePickerView.show();
            }
        });

        rl_unit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(unit.size() == 0){
                    return;
                }

                simplePickerView = new SimplePickerView(mContext, AddrSearchView.this, unit , rl_unit_text, "单元");
                simplePickerView.setCancelable(true);
                simplePickerView.show();
            }
        });

        rl_room_text.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                curRoom = s.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //clear
        rl_clear_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reset();
                //currentSessionFragment.getAdapter().getFilter().filter("");
            }
        });

        rl_ok_data_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                curBuilding = rl_building_text.getText().toString();
                curUnit = rl_unit_text.getText().toString();
                curRoom = rl_room_text.getText().toString();

                if(showStatus){
                    if(rl_radio0.isChecked()){
                        curRoom = "inService";
                    }else if(rl_radio1.isChecked()){
                        curRoom = "notInService";
                    }else if(rl_radio2.isChecked()){
                        curRoom = "isRest";
                    }
                }

                hideSearchView();

                String filterStr = curCity + curVillage + curBuilding.replace("请选择","") + curUnit.replace("请选择", "") + curRoom;

                if (mParent instanceof searchListener){
                    ((searchListener)mParent).addrSearchRes(filterStr, curCity, curCityPinyin, curVillage, curBuilding.replace("请选择",""), curUnit.replace("请选择", ""), curRoom);
                }

                hideSoftKeyboard();

                //currentSessionFragment.getAdapter().getFilter().filter(filterStr);
            }
        });


        getVillageData();

        decorView.addView(rootView);

        showView();

        addLayoutListener(rootView, rl_ok_data_btn);
    }

    public void reset(){
        clearViewData();

        if (mParent instanceof searchListener){
            ((searchListener)mParent).addrSearchRes("", "", "", "", "", "", "");
        }

        hideSoftKeyboard();
    }

    void hideSoftKeyboard() {
        if (((Activity)mContext).getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (((Activity)mContext).getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(((Activity)mContext).getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void clearViewData(){

        RadioGroup radiogroup = (RadioGroup) rootView.findViewById(R.id.radiogroup);
        TextView rl_building_text = (TextView) rootView.findViewById(R.id.tv_building_text);
        TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
        EditText rl_room_text = (EditText) rootView.findViewById(R.id.tv_room_text);

        clearVillBtn();
        curCity = "";
        curVillage = "";
        curCityPinyin = "";
        rl_building_text.setText("请选择");
        rl_unit_text.setText("请选择");
        rl_room_text.setText("");
        building.clear();
//        building.add("请选择");
        unit.clear();
//        unit.add("请选择");

        rl_building_text.setTextColor(Color.parseColor("#B5B5B5"));
        rl_unit_text.setTextColor(Color.parseColor("#B5B5B5"));


        radiogroup.clearCheck();
    }

    public void showView() {
        rootView.setVisibility(View.VISIBLE);
    }

    private void getVillageData() {
        ApiService apiService = HKHttpUtils.getApiService();
        retrofit2.Call<BaseResponse<List<AdminVillage>>> call = apiService.getAllCityVillage(MainActivity.ticket);
        call.enqueue(new HKResponseResultCallBack<List<AdminVillage>>() {
            @Override
            public void onSuccess(List<AdminVillage> adminVillages) {
                if (adminVillages != null && adminVillages.size() != 0) {
                    mList.clear();
                    mList.addAll(adminVillages);
                    mAdapter.notifyDataSetChanged();

                }else{
                    mList.clear();
                }
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(mContext, "请求失败:"+msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(retrofit2.Call call, Throwable t) {
                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
//        OkHttpClient okHttpClient = new OkHttpClient();
//
//        RequestBody body = new FormBody.Builder()
//                .build();
//
//        Request request = new Request.Builder()
//                .url(BuildConfig.API_URL + "admin/getAllCityVillage")
//                .post(body)
//                .build();
//
//        Call call = okHttpClient.newCall(request);
//
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
//                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//                final String res = response.body().string();
//                final AdminVillageRes result = CommonUtils.parseJsonWithGson(res, AdminVillageRes.class);
//                ((Activity)mContext).runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
////                        hideLoading();
//                        if(result.getCd() == 1){
//                            return;
//                        }
//
//                        List<AdminVillage> dataList = result.getData();
//                        if(dataList.size() != 0){
//                            mList.clear();
//                            mList.addAll(dataList);
//                            mAdapter.notifyDataSetChanged();
//                        }else{
//                            mList.clear();
//                        }
//                    }
//                });
//
//            }
//        });

    }

    private void getBuilding(){
        ApiService apiService = HKHttpUtils.getApiService();
        retrofit2.Call<BaseResponse<ArrayList<String>>> call = apiService.getHomeBuildings(MainActivity.ticket, curCity, curVillage);
        call.enqueue(new HKResponseResultCallBack<ArrayList<String>>() {
            @Override
            public void onSuccess(ArrayList<String> strings) {
                if (strings != null){
                    building.addAll(strings);
                    if(building.size() > 0){
                        TextView rl_building_text = (TextView) rootView.findViewById(R.id.tv_building_text);
                        rl_building_text.setTextColor(Color.parseColor("#565656"));
                    }
                }
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(mContext, "请求失败:"+msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(retrofit2.Call call, Throwable t) {
                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
//        OkHttpClient okHttpClient = new OkHttpClient();
//
//        RequestBody body = new FormBody.Builder()
//                .add("city", curCity)
//                .add("homeNeighbourhoods", curVillage)
//                .build();
//
//        Request request = new Request.Builder()
//                .url(BuildConfig.API_URL + "account/listHomeBuilding")
//                .post(body)
//                .build();
//
//        Call call = okHttpClient.newCall(request);
//
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
//                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//                final String res = response.body().string();
//                final UserBuildRes result = CommonUtils.parseJsonWithGson(res, UserBuildRes.class);
//                ((Activity)mContext).runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
////                        hideLoading();
//                        if(result.getCd() == 1){
//                            return;
//                        }
//
////                        building = result.getData();
//
//                        building.addAll(result.getData());
//                        if(building.size() > 0){
//                            TextView rl_building_text = (TextView) rootView.findViewById(R.id.tv_building_text);
//                            rl_building_text.setTextColor(Color.parseColor("#565656"));
//                        }
//
////                        RelativeLayout rl_unit = (RelativeLayout) rootView.findViewById(R.id.rl_unit);
////                        RelativeLayout rl_room = (RelativeLayout) rootView.findViewById(R.id.rl_room);
////                        if(building.size() > 0){
////                            rl_building_text.setVisibility(View.VISIBLE);
////                            rl_unit.setVisibility(View.GONE);
////                            rl_room.setVisibility(View.GONE);
////                        }else{
////                            rl_building_text.setVisibility(View.GONE);
////                            rl_unit.setVisibility(View.GONE);
////                            rl_room.setVisibility(View.GONE);
////                        }
//                    }
//                });
//
//            }
//        });
    }

    public void getUnit(String build){
        ApiService apiService = HKHttpUtils.getApiService();
        retrofit2.Call<BaseResponse<ArrayList<String>>> call = apiService.getHomeUnits(MainActivity.ticket, curCity, curVillage, build);
        call.enqueue(new HKResponseResultCallBack<ArrayList<String>>() {
            @Override
            public void onSuccess(ArrayList<String> strings) {
                if (strings != null){
                    unit.clear();
//                        unit.add("请选择");
                    unit.addAll(strings);
                    if(unit.size() > 0){
                        TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
                        rl_unit_text.setTextColor(Color.parseColor("#565656"));
                    }
                }
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Toast.makeText(mContext, "请求失败:"+msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(retrofit2.Call call, Throwable t) {
                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
//        OkHttpClient okHttpClient = new OkHttpClient();
//
//        RequestBody body = new FormBody.Builder()
//                .add("city", curCity)
//                .add("homeNeighbourhoods", curVillage)
//                .add("homeBuilding", build)
//                .build();
//
//        Request request = new Request.Builder()
//                .url(BuildConfig.API_URL + "account/listHomeUnit")
//                .post(body)
//                .build();
//
//        Call call = okHttpClient.newCall(request);
//
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
//                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//                final String res = response.body().string();
//                final UserBuildRes result = CommonUtils.parseJsonWithGson(res, UserBuildRes.class);
//                ((Activity)mContext).runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
////                        hideLoading();
//                        if(result.getCd() == 1){
//                            return;
//                        }
//
////                        unit = result.getData();
//                        unit.clear();
////                        unit.add("请选择");
//                        unit.addAll(result.getData());
//                        if(unit.size() > 0){
//                            TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
//                            rl_unit_text.setTextColor(Color.parseColor("#565656"));
//                        }
//
//
////                        RelativeLayout rl_unit = (RelativeLayout) rootView.findViewById(R.id.rl_unit);
////                        RelativeLayout rl_room = (RelativeLayout) rootView.findViewById(R.id.rl_room);
////                        if(unit.size() > 0){
////                            rl_unit.setVisibility(View.VISIBLE);
////                            rl_room.setVisibility(View.VISIBLE);
////                        }else{
////                            rl_unit.setVisibility(View.GONE);
////                            rl_room.setVisibility(View.GONE);
////                        }
//                    }
//                });
//
//            }
//        });
    }


    public boolean isShowing() {
        return rootView.getVisibility() == View.VISIBLE;
    }

    public void hideSearchView(){
        rootView.setVisibility(View.GONE);
    }

    private void clearVillBtn(){
        if (rootView == null){
            return;
        }

        ListView villageList = (ListView) rootView.findViewById(R.id.village_list);
        for (int i = 0; i < villageList.getChildCount(); i++) {
            NoScrollGridView noScrollGridView = (NoScrollGridView)villageList.getChildAt(i).findViewById(R.id.building_ist);
            for (int j = 0; j < noScrollGridView.getChildCount(); j++) {
                LinearLayout all = (LinearLayout) noScrollGridView.getChildAt(j);
                TextView t = (TextView)all.findViewById(R.id.grid_item_text);
                t.setBackgroundResource(R.drawable.btn_select_selector);
                t.setTextColor(Color.parseColor("#565656"));
            }
        }
    }

    /**
     * 初始化View
     */
    private static class ViewHolder
    {
        private TextView titleTextView;
        private NoScrollGridView  contentGridView;
        private GridViewAdapter gridViewAdapter;
    }

    /**
     * 适配器
     */
    private class VillageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public AdminVillage getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_building_list, null);
                viewHolder = new ViewHolder();
                viewHolder.titleTextView = (TextView)convertView.findViewById(R.id.tv_city);
                viewHolder.contentGridView = (NoScrollGridView)convertView.findViewById(R.id.building_ist);
                viewHolder.gridViewAdapter = new GridViewAdapter();
                viewHolder.contentGridView.setAdapter(viewHolder.gridViewAdapter);
                convertView.setTag(viewHolder);

            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            AdminVillage adminVillage = mList.get(position);

            viewHolder.titleTextView.setText(adminVillage.getName() + ":");
            viewHolder.titleTextView.setTag(adminVillage.getKey());

            final List<String> adminVillList = new ArrayList<>();
            for (int i = 0; i < adminVillage.getVillageList().size(); i++) {
//                if(viewHolder.contentGridView.getChildCount() < adminVillage.getVillageList().size()){
//
//                }

                adminVillList.add(adminVillage.getVillageList().get(i).getName());
            }

            viewHolder.gridViewAdapter.setList(adminVillList);
            viewHolder.gridViewAdapter.notifyDataSetChanged();

            viewHolder.contentGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position2,
                                        long id) {

                    clearVillBtn();

                    TextView rl_building_text = (TextView) rootView.findViewById(R.id.tv_building_text);
                    TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
                    EditText rl_room_text = (EditText) rootView.findViewById(R.id.tv_room_text);
                    View rl_building_line = rootView.findViewById(R.id.rl_building_line);
                    View rl_building = rootView.findViewById(R.id.rl_building);
                    View rl_unit_line = rootView.findViewById(R.id.rl_unit_line);
                    View rl_unit = rootView.findViewById(R.id.rl_unit);
                    View rl_room_line = rootView.findViewById(R.id.rl_room_line);
                    View rl_room = rootView.findViewById(R.id.rl_room);
                    if (mList != null && mList.size() != 0){
                        AdminVillage villages = mList.get(position);
                        if (villages != null && villages.getVillageList().size() != 0){
                            AdminVillage village = villages.getVillageList().get(position2);
                            if (village.getIsOpen() == 1){
                                rl_building_line.setVisibility(View.GONE);
                                rl_building.setVisibility(View.GONE);
                                rl_unit_line.setVisibility(View.GONE);
                                rl_unit.setVisibility(View.GONE);
                                rl_room_line.setVisibility(View.GONE);
                                rl_room.setVisibility(View.GONE);
                            }else{
                                rl_building_line.setVisibility(View.VISIBLE);
                                rl_building.setVisibility(View.VISIBLE);
                                rl_unit_line.setVisibility(View.VISIBLE);
                                rl_unit.setVisibility(View.VISIBLE);
                                rl_room_line.setVisibility(View.VISIBLE);
                                rl_room.setVisibility(View.VISIBLE);
                            }
                        }
//                        if (mList.get(position).getVillageList().get(position2))
                    }


                    rl_building_text.setText("请选择");
                    rl_building_text.setTextColor(Color.parseColor("#B5B5B5"));
                    rl_unit_text.setText("请选择");
                    rl_unit_text.setTextColor(Color.parseColor("#B5B5B5"));
                    rl_room_text.setText("");

                    LinearLayout taget = (LinearLayout) view;
                    TextView rb = (TextView)taget.findViewById(R.id.grid_item_text);
                    rb.setTextColor(Color.parseColor("#ffffff"));
                    rb.setBackgroundResource(R.drawable.btn_bg_shape_hk_selected);
                    curVillage = adminVillList.get(position2);
                    curCity = viewHolder.titleTextView.getText().toString().replace(":","");
                    curCityPinyin = viewHolder.titleTextView.getTag().toString();
                    building.clear();
//                    building.add("请选择");
                    unit.clear();
//                    unit.add("请选择");

                    getBuilding();
                }
            });

            return convertView;
        }
    }


    /**
     * 适配器
     */
    private class GridViewAdapter extends BaseAdapter {

        private List<String> list = new ArrayList<>();

        public void setList(List<String> list){
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.gridview_item, null);
            }

            TextView village = (TextView)convertView.findViewById(R.id.grid_item_text);

            String villagename = list.get(position);
            village.setText(villagename);

            return convertView;
        }
    }

    @Override
    public void simplePickerSelect(int position, ArrayList<String> valueArray, View view) {
        String build = valueArray.get(position);
        TextView textView = (TextView)view;
        String tag = textView.getTag().toString();
        textView.setText(build);

        if(tag.equals("build")){
            TextView rl_unit_text = (TextView) rootView.findViewById(R.id.tv_unit_text);
            EditText rl_room_text = (EditText) rootView.findViewById(R.id.tv_room_text);
            rl_unit_text.setText("请选择");
            rl_room_text.setText("");
            getUnit(build);

        }else if(tag.equals("unit")){
            EditText rl_room_text = (EditText) rootView.findViewById(R.id.tv_room_text);
            rl_room_text.setText("");
        }
    }

    public interface searchListener{
        void addrSearchRes(String search, String city, String curCityPinyin, String village, String building, String unit, String edit_str);
    }
}
